# _*_ coding:utf-8 _*_
# @Time :2021/3/22 10:17
# @Author :yanxia

from application.libs import const
from application import db
from application.apps.collect.service.emial_collect import check_report_recipient, check_test_emaildata, \
    send_email_html, check_report_emaildata, get_data_standand, check_report_cc
from application.models.models import TestReportTable



def send_report_email(data):
    pid=int(data["pid"])
    testReportTable = db.session.query(TestReportTable).filter(TestReportTable.id == pid).first()
    if not testReportTable:
        return const.OPERATE_FAIL
    receiver_list = check_report_recipient(pid)
    if not isinstance(receiver_list, list):
        raise TypeError
    cc_list = check_report_cc(pid)
    if not isinstance(cc_list, list):
        raise TypeError
    report_html="""
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>报告</title>
</head>
<body>
<div style="clear:both;">
    <span style="display:none;" class="aym_bookmark" id="_aym_bookmark_sqm_1657"></span>
    <p style="margin:.0px .0px .0px;text-align:justify;font-family:Calibri;font-size:14.0px;line-height:29.0px;"
       class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                               class=" __aliyun_node_has_color">Dear All，</span></p>
    <p style="margin:.0px .0px .0px;text-align:justify;font-family:Calibri;font-size:14.0px;text-indent:28.0px;line-height:29.0px;"
       class="MsoNormal"><span style="font-family:微软雅黑;color:#000000;font-size:14.0px;"
                               class=" __aliyun_node_has_color">本版本</span><span
            style="font-family:微软雅黑;color:#000000;font-size:14.0px;"
            class=" __aliyun_node_has_color">测试工作已完成，情况如下：</span></p>
    <p style="margin:.0px .0px .0px;text-align:justify;font-family:Calibri;font-size:14.0px;text-indent:28.0px;line-height:29.0px;"
       class="MsoNormal"><span style="font-family:微软雅黑;color:#000000;font-size:14.0px;"
                               class=" __aliyun_node_has_color">&nbsp;</span></p>
    <div class="aym_table_wrap"
         style="overflow: hidden; display: table; font-family: &quot;Times New Roman&quot;; font-size: 13.3px; line-height: 22.61px; width: 548px; height: 530px; margin: 0px;">
        <table style="font-family:Times New Roman;font-size:13.3px;border-collapse:collapse;margin-left:9.0px;margin-right:9.0px;border:none;"
               border="0" cellspacing="0" class="MsoNormalTable">
            <tbody>
            <tr>
                <td style="padding:.0px 7.0px;border-width:1.0px;border-style:solid;border-color:windowtext;background:#f2f4f7;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="187"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">测试周期：</span></p></td>
                <td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:1.0px solid windowtext;border-bottom:1.0px solid windowtext;background:#ffffff;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="312"><p
                        style="margin: 0px; text-align: justify; font-family: Calibri; font-size: 14px; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">%(begin_time)s 至 %(end_time)s(%(several_days)d人.天)</span>
                </p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="187"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">产品经理：</span></p></td>
                <td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="312"><p
                        style="margin: 0px; text-align: justify; font-size: 14px; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-family:微软雅黑;">%(pro_manager)s</span></p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="187"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">开发人员：</span></p></td>
                <td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="312"><p
                        style="margin: 0px; text-align: justify; font-size: 14px; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-family:微软雅黑;">%(developer)s</span></p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="187"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">QA：</span></p></td>
                <td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="312"><p
                        style="margin: 0px; text-align: justify; font-size: 14px; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-family:微软雅黑;">%(qa)s</span></p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f4f5f7;"
                    class=" __aliyun_node_has_bgcolor" colspan="2" valign="center" width="499"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: center; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:15.0px;"
                                                class=" __aliyun_node_has_color">Bug概括</span></p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="187"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">Bug数量：</span></p></td>
                <td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="312"><p
                        style="margin: 0px; text-align: justify; font-family: Calibri; font-size: 14px; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">%(bug_counts)d</span></p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="187"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">已修复Bug数：</span></p></td>
                <td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="312"><p
                        style="margin: 0px; text-align: justify; font-family: Calibri; font-size: 14px; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">%(bug_repair)d</span></p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="187"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">遗留Bug数：</span></p></td>
                <td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="312"><p
                        style="margin: 0px; text-align: justify; font-family: Calibri; font-size: 14px; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">%(bug_legacy)d</span></p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="187"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">建议/用户体验问题数量：</span></p></td>
                <td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="312"><p
                        style="margin: 0px; text-align: justify; font-family: Calibri; font-size: 14px; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">%(advice_counts)d</span></p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f4f5f7;"
                    class=" __aliyun_node_has_bgcolor" colspan="2" valign="center" width="499"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: center; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:15.0px;"
                                                class=" __aliyun_node_has_color">质量数据（</span><span
                        style="font-family:微软雅黑;font-weight:700;font-size:15.0px;"><a href="#/qualitymanagement/qulity">说明</a></span>）
                </p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="187"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">Bug密度(标准值：%(bugmid)d</span><span
                        style="font-weight:700;font-family:Calibri;color:#000000;font-size:14.0px;"
                        class=" __aliyun_node_has_color">)</span><span
                        style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                        class=" __aliyun_node_has_color">：</span></p></td>
                <td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="312"><p
                        style="margin: 0px; text-align: justify; font-family: Calibri; font-size: 14px; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">%(bug_density)d</span></p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="187"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">Bug引入率(标准值：%(introerror)d</span><span
                        style="font-weight:700;font-family:Calibri;color:#000000;font-size:14.0px;"
                        class=" __aliyun_node_has_color">)</span><span
                        style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                        class=" __aliyun_node_has_color">：</span></p></td>
                <td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="312"><p
                        style="margin: 0px; text-align: justify; font-family: Calibri; font-size: 14px; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">%(bug_introd)d</span></p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="187"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">结论：</span></p></td>
                <td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="312"><p
                        style="margin: 0px; text-align: justify; font-family: Calibri; font-size: 14px; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#00b050;font-size:19.0px;"
                                                class=" __aliyun_node_has_color">%(conclusion)s</span></p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f4f5f7;"
                    class=" __aliyun_node_has_bgcolor" colspan="2" valign="center" width="499"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: center; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:15.0px;"
                                                class=" __aliyun_node_has_color">问题列表</span></p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="187"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">Mantis地址：</span></p></td>
                <td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="312"><p
                        style="margin: 0px; text-align: justify; font-family: Calibri; font-size: 14px; line-height: 29px; height: auto;"
                        class="MsoNormal"><a
                        href="%(mantis_adress)s"
                        target="_blank"><span style="font-family:Calibri;font-size:14.0px;">点击我跳转</span></a></p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="187"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">Mantis访问帐号\密码：</span></p></td>
                <td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="312"><p
                        style="margin: 0px; text-align: justify; font-family: Calibri; font-size: 14px; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">guest\guest</span></p></td>
            </tr>
            <tr>
                <td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="187"><p
                        style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
                        class="MsoNormal"><span style="font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
                                                class=" __aliyun_node_has_color">备注：</span></p></td>
                <td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
                    class=" __aliyun_node_has_bgcolor" valign="top" width="312"><p
                        style="margin: 0px; text-align: justify; font-family: Calibri; font-size: 14px; line-height: 29px; height: auto;"
                        class="MsoNormal"><span>%(remarks)s</span></p></td>
            </tr>
            </tbody>
        </table>
    </div>
    <p style="margin:.0px .0px .0px;text-align:justify;font-family:Calibri;font-size:14.0px;line-height:29.0px;"
       class="MsoNormal"><span style="font-family:Calibri;font-size:14.0px;">&nbsp;</span></p>
    <span style="display:none;" class="aym_bookmark" id="_aym_bookmark_sqm_1658"></span>
</div>

</body>
</html>
    """

    data=check_report_emaildata(pid)
    standard=get_data_standand()
    bugmid=standard[0]
    introerror=standard[1]

    data_html=report_html %{'begin_time':data["begin_time"],'end_time':data["end_time"],'several_days':data["several_days"],
                            'pro_manager':data["pro_manager"],'developer':data["developer"],'qa':data["qa"],
                            'bug_counts':data["bug_counts"],'bug_repair':data["bug_repair"],'bug_legacy':data["bug_legacy"],
                            'advice_counts':data["advice_counts"],'bugmid':bugmid,'bug_density':data["bug_density"],
                            'bug_introd':data["bug_introd"],'introerror':introerror,'conclusion':data["conclusion"],
                            'mantis_adress':data["mantis_adress"],'remarks':data["remarks"]}
    msg = const.OPERATE_SUCCESS
    try:
        send_email_html(receiver_list,cc_list, "【验收报告】" +data["project_name"], data_html)
    except:
        msg = const.OPERATE_FAIL
    return msg